import { Component, ViewChild, Injector, Output, EventEmitter, ElementRef } from '@angular/core';
import { ModalDirective } from 'ngx-bootstrap';
import { TechFileInfoListDto, TechFileInfoServiceProxy, AlterationInvoiceItemDto } from '@shared/service-proxies/service-proxies';
import { AppComponentBase } from '@shared/common/app-component-base';
import { Paginator, LazyLoadEvent } from 'primeng/primeng';
import { Table } from 'primeng/table';
import { PrimengTableHelper } from '@shared/helpers/PrimengTableHelper';

@Component({
  selector: 'AddAlterItem',
  templateUrl: './add-alterationinvoice-item.component.html'
})

export class AddAlterItemComponent extends AppComponentBase {
  @ViewChild('actionModal') modal: ModalDirective;
  @ViewChild('dataTable') dataTable: Table;
  @ViewChild('paginator') paginator: Paginator;
  @ViewChild('dataTable1') dataTable1: Table;
  @ViewChild('paginator1') paginator1: Paginator;

  primengTableHelper1 = new PrimengTableHelper();
  @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

  active = false;
  saving = false;
  filterText = '';
  selectedpick: TechFileInfoListDto[];
  orderitems: Array<AlterationInvoiceItemDto>;

  constructor(
    injector: Injector,
    private _techServiceProxy: TechFileInfoServiceProxy,
  ) {
    super(injector);
  }

  show(orderitems: Array<AlterationInvoiceItemDto>): void {
    this.active = true;
    this.orderitems = orderitems ? orderitems : new Array<AlterationInvoiceItemDto>();
    this.getModels();
    this.getModels1();
    this.modal.show();
  }

  getModels(event?: LazyLoadEvent) {
    setTimeout(() => {
      if (this.primengTableHelper.shouldResetPaging(event)) {
        this.paginator.changePage(0);

        return;
      }

      this.primengTableHelper.showLoadingIndicator();

      this._techServiceProxy
        .getTechFileInfos(
          this.filterText,
          undefined,
          undefined,
          undefined,
          true,
          null,
          null,
          this.primengTableHelper.getSorting(this.dataTable),
          this.primengTableHelper.getMaxResultCount(this.paginator, event),
          this.primengTableHelper.getSkipCount(this.paginator, event)
        )
        .subscribe(result => {
          this.primengTableHelper.totalRecordsCount = result.totalCount;
          this.primengTableHelper.records = [];
          var templist = result.items;
          templist.forEach(element => {
            if (!this.orderitems.find(o => (o.newTechFileInfo.id ? o.newTechFileInfo.id : "") == element.id))
              this.primengTableHelper.records.push(element);
            else
              this.primengTableHelper.totalRecordsCount--;
          });
          this.primengTableHelper.hideLoadingIndicator();
        });
    }, 0);
  }

  getModels1(event?: LazyLoadEvent) {
    setTimeout(() => {
      if (this.primengTableHelper1.shouldResetPaging(event)) {
        this.paginator.changePage(0);
        return;
      }

      this.primengTableHelper1.showLoadingIndicator();

      this._techServiceProxy
        .getTechFileInfos(
          this.filterText,
          undefined,
          undefined,
          undefined,
          false,
          null,
          null,
          this.primengTableHelper1.getSorting(this.dataTable1),
          this.primengTableHelper1.getMaxResultCount(this.paginator1, event),
          this.primengTableHelper1.getSkipCount(this.paginator1, event)
        )
        .subscribe(result => {
          this.primengTableHelper1.totalRecordsCount = result.totalCount;
          this.primengTableHelper1.records = result.items;
          this.primengTableHelper1.hideLoadingIndicator();
        });
    }, 0);
  }

  save(): void {
    this.saving = true;
    let alteritems;
    if (this.selectedpick) {
      alteritems = this.selectedpick.map(s => {
        let alter = new AlterationInvoiceItemDto();
        alter.oldTechFileInfo = s;
        alter.oldTechFileInfoId = s.id;
        return alter;
      });
    }
    this.close();
    this.modalSave.emit(alteritems);
  }

  search() {
    this.paginator.changePage(this.paginator.getPage());
    this.paginator1.changePage(this.paginator1.getPage());
  }

  close(): void {
    this.saving = false;
    this.modal.hide();
  }
}

